var form, $,areaData;
layui.config({
    base : "../../js/"
}).extend({
    "address" : "address"
})
layui.use(['form','layer','upload','laydate',"address"],function(){
    form = layui.form;
    $ = layui.jquery;
    var layer = parent.layer === undefined ? layui.layer : top.layer,
        upload = layui.upload,
        laydate = layui.laydate,
        address = layui.address;

    //获取用户信息
    $(function () {
        $.ajax({
            url: "/user/getUserById",
            type: 'get',
            success: function (result) {
                if (result!=null){
                    var res = result.data;
                    $("#userId").val(res.userId);
                    $("#loginName").val(res.loginName);
                    $("#userName").val(res.userName);
                    $("#phonenumber").val(res.phonenumber);
                    $("#email").val(res.email);
                    $("#userFace").attr('src',res.avatar);
                }
            }
        });
    })

    //上传头像
    upload.render({
        url: '/user/uploadAvatar',
        type : 'get',
        elem: '#userFace',
        done: function(res){
            //如果上传失败
            if(res.response == 'success'){
                var src="https://"+res.name;
                $('#userFace').attr('src',src);
                parent.location.reload();
            }else
            {
                return layer.msg('上传失败');
            }
            //上传成功
        }
    });

    //监听修改用户信息的提交按钮
    form.on('submit(changeUser)', function (data) {
        $.ajax({
            url: '/user/update',
            dataType: 'json',
            type: 'post',
            contentType: "application/json;charset=UTF-8",
            data:  JSON.stringify(data.field),
            success: function (result) {
                layer.msg("操作成功");
                location.reload();
            },
            error:function (result) {
                layer.msg("操作失败");
                location.reload();
            }
        });
        return false;
    });

})